package com.loma.app2

import android.content.ComponentName
import android.content.Intent
import android.content.ServiceConnection
import android.os.Bundle
import android.os.IBinder
import android.os.RemoteException
import android.util.Log
import android.view.View
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
import com.loma.aidl_demo.IMyAidlInterface

class MainActivity : AppCompatActivity(), ServiceConnection {
    var serviceIntent: Intent? = null
    lateinit var edit: EditText
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        edit = findViewById(R.id.text)
        serviceIntent = Intent()
        serviceIntent!!.component = ComponentName("com.loma.aidl_demo", "com.loma.aidl_demo.MyService")
    }

    fun startService(view: View?) {
        bindService(serviceIntent, this, BIND_AUTO_CREATE)
    }

    fun stopService(view: View?) {
        unbindService(this)
        binder = null
    }

    override fun onServiceConnected(name: ComponentName, service: IBinder) {
        Log.e("zjt",service.toString())
        binder = IMyAidlInterface.Stub.asInterface(service)
    }
    override fun onServiceDisconnected(name: ComponentName) {}

    var binder :IMyAidlInterface?=null
    fun send(view: View) {
        if (binder!=null){
            try {
                binder!!.setData(edit.text.toString())
            }catch ( e:RemoteException ){
                e.printStackTrace()
            }

        }
    }
}